package com.act.moudles.user.controller;


import com.act.moudles.user.entity.Menu;
import com.act.moudles.user.service.MenuService;
import com.act.utils.DataResult;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import javax.annotation.Resource;

/**
 * 菜单权限表Controller
 * @author monxz
 * @version 2023-02-06
 */

@RestController
@RequestMapping(value = "user/menu")
@Api(tags = "菜单权限表")
@Slf4j
public class MenuController  {

	@Resource
	private MenuService menuService;

    @PostMapping("treeTable")
    @ApiOperation("菜单树")
	public DataResult treeTable(@RequestBody Menu menu){
        log.info("查询参数：{}", JSON.toJSONString(menu));
        return DataResult.success("查询成功",menuService.buildMenuTree(menu));
    }

	@PostMapping("add")
    @ApiOperation("新增菜单权限表")
    public DataResult add(@RequestBody Menu menu){
        log.info("新增参数：{}", JSON.toJSONString(menu));
        //新增
        String addRes = menuService.addMenu(menu);
        if(StringUtils.isNotEmpty(addRes)){
            return DataResult.fail(addRes);
        }
        return DataResult.success("新增成功");
    }

    @PostMapping("modify")
    @ApiOperation("修改菜单权限表")
    public DataResult modify(@RequestBody Menu menu){
        log.info("修改参数：{}", JSON.toJSONString(menu));
        // 校验接口参数
        //修改
        String updRes = menuService.updateMenu(menu);
        if(StringUtils.isNotEmpty(updRes)){
            return DataResult.fail(updRes);
        }
        return DataResult.success("修改成功");
    }

    @GetMapping("removeMenu")
    @ApiOperation("删除菜单")
    public DataResult  removeMenu(Long menuId){
        String removeRes = menuService.removeMenu(menuId);
        if(!StringUtils.isEmpty(removeRes)){
            return DataResult.fail(removeRes);
        }
        return DataResult.success("删除成功");
    }


}